﻿using System;
using System.Text;
using HZ.BidSystem.Model;
using System.Web;

public partial class CheckLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //try
        //{
        //获取客户端数据
        string radomnumber = (string)Session["ranNum"];
        string signdata = Request.Form["id"];
        signdata = signdata.Replace(' ', '+');
        //将随机数按照参数格式进行utf-8编码 
        radomnumber = "=?UTF-8?B?" + Convert.ToBase64String(Encoding.UTF8.GetBytes(radomnumber)) + "?=";
        EZCAService.SignatureServerWebServicePortTypeClient sw = new EZCAService.SignatureServerWebServicePortTypeClient();
        string ret = sw.VD_GetUserIdByLogin(radomnumber, signdata);
        //返回错误异常处理
        string error = "";
        //判断登录有效性 
        if (ret.Equals("10001"))
        {
            error = "无法解析传入的Base64编码数据";
            Response.Redirect("UserLoginCA.aspx?error=" + error, false);
        }
        else if (ret.Equals("10002"))
        {
            error = "签名验证失败";
            Response.Redirect("UserLoginCA.aspx?error=" + error, false);
        }
        else if (ret.Equals("10006"))
        {
            error = "用户证书已被吊销";
            Response.Redirect("UserLoginCA.aspx?error=" + error, false);
        }
        else if (ret.Equals("10010"))
        {
            error = "用户证书已被冻结";
            Response.Redirect("UserLoginCA.aspx?error=" + error, false);
        }
        else if (ret.Equals("10003"))
        {
            error = "证书未在CA服务器绑定 ";
            Response.Redirect("UserLoginCA.aspx?error=" + error, false);
        }
        else if (ret.Equals("10004"))
        {
            error = "证书过期";
            Response.Redirect("UserLoginCA.aspx?error=" + error, false);
        }
        else if (ret.Equals("10005"))
        {
            error = "数据签名包异常";
            Response.Redirect("UserLoginCA.aspx?error=" + error, false);
        }
        else
        {
            Account account = null;
            int flag = BLL_Factory.IBLL_Common.Login(ret, ret, out account);
            Random random = new Random();
            if (flag == 1)
            {
                CurrentAccount.Account = account;
                //目前只有企业用key登录，后期其它角色用key登录再放开
                 
                string ip = GetIP();
                HZ.Common.log4netManager.LogInfo("登录系统", string.Format("登录成功:用户编号{0},登录IP地址{1}", account.AccountName, ip), null);
                

                if (account.AccountType.Value == 2)//企业
                { 
                    Response.Redirect("~/Company/Default.aspx");
                }
                else if (account.AccountType == 1 || account.AccountType == 0)//管理员
                {
                    Response.Redirect("~/Manage/Default.aspx");
                }
                else if (account.AccountType == 3) //专家类型
                {
                    Response.Redirect("~/Manage/ExpertDefault.aspx");
                }
                else
                {
                    HZ.Web.WebUtil.Alert("禁止该类型账户登录系统！");
                    return;
                } 
            }
            else
            {
                error = "系统用户不存在！";
                Response.Redirect("UserLoginCA.aspx?error=" + error, false);
            }
        }
        //}catch(Exception ex)
        //{
        //    string erroe = "用户不存在";
        //    Response.Redirect("Login.aspx?error=" + erroe);
        //    throw ex;
        //}

    }
     /// <summary>
     /// 获取客户端IP地址（无视代理）
     /// </summary>
     /// <returns>若失败则返回回送地址</returns>
    public static string GetIP() 
    {
        string userHostAddress = HttpContext.Current.Request.UserHostAddress;

        if (string.IsNullOrEmpty(userHostAddress))
        {
            userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }

        //最后判断获取是否成功，并检查IP地址的格式（检查其格式非常重要）
        if (!string.IsNullOrEmpty(userHostAddress) && IsIP(userHostAddress))
        {
            return userHostAddress;
        }
        return "127.0.0.1";
    }

    /// <summary>
    /// 检查IP地址格式
    /// </summary>
    /// <param name="ip"></param>
    /// <returns></returns>
    public static bool IsIP(string ip)
    {
        return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
    }
}
